﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorldBankBBS.Data;
using WorldBankBBS.Menus;
using WorldBankBBS.Subboards;
using Session = WorldBankBBS.Logic.Session;

namespace WorldBankBBS.Subboards
{
    public class SubboardsMenuProvider : IMenuProvider
    {
        public SubboardsMenuProvider()
        {
            MenuItems = new List<MenuItem>(
                new[]
                {
                    new MenuItem
                    {
                        Abbreviation = "P",
                        Full = "POST",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:CreateMessage",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "CT",
                        Full = "CREATE THREAD",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:CreateThread",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "#",
                        Full = "READ",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ReadMessage|{0}",
                        TakesParameters = true,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "DC",
                        Full = "DELETE CURRENT",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:DeleteMessage|{0}",
                        TakesParameters = false,
                        UseCurrentContext = true,
                    },
                    new MenuItem
                    {
                        Abbreviation = "D",
                        Full = "DELETE ",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:DeleteMessage|{0}",
                        TakesParameters = true,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "RC",
                        Full = "REPLY CURRENT",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:CreateMessage|{0}",
                        TakesParameters = false,
                        UseCurrentContext = true,
                    },
                    new MenuItem
                    {
                        Abbreviation = "R",
                        Full = "REPLY ",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:CreateMessage|{0}",
                        TakesParameters = true,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "LM",
                        Full = "LIST",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ListMessages",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "U",
                        Full = "UNREAD",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ListUnreadMessages",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "LT",
                        Full = "THREADS",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ListThreads",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "T",
                        Full = "THREAD ",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ChangeThread|{0}",
                        TakesParameters = false,
                        TakesThreadParameter = true,
                        TakesSubboardParameter = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "LS",
                        Full = "SUBBOARDS",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ListSubboards",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "NS",
                        Full = "NEW SCAN",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:NewScan",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "S",
                        Full = "SUBBOARD ",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ChangeSubboard|{0}",
                        TakesParameters = false,
                        TakesThreadParameter = false,
                        TakesSubboardParameter = true,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "Q",
                        Full = "QUIT",
                        ResourcePattern = "",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                    new MenuItem
                    {
                        Abbreviation = "",
                        Full = "",
                        ResourcePattern = "WorldBankBBS.Subboards:WorldBankBBS.Subboards.SubboardPathHandler:ReadNextMessage",
                        TakesParameters = false,
                        UseCurrentContext = false,
                    },
                }
                );

            Prompt = SubboardStrings.SUBBOARD_PROMPT;

            ShortHelpFilename = "subboard_help_short";
            LongHelpFilename = "subboard_help_long";

            GenericHelpText = SubboardStrings.SUBBOARD_GENERIC_HELP;

        }

        public IList<MenuItem> MenuItems { get; private set; }
        public string ShortHelpFilename { get; private set; }
        public string LongHelpFilename { get; private set; }
        public string GenericHelpText { get; private set; }
        public string Prompt { get; private set; }

        public void Refresh(Session session)
        {
            var items = SubboardsHandler.ListMessages(session, false);
            var lookups = new Dictionary<string, Guid>();
            var count = 1;
            foreach (var l in items)
            {
                lookups.Add((count++).ToString(), l.MessageUID);
            }

            var threads = SubboardsHandler.ListThreads(session, false);
            var threadsLookup = new Dictionary<string, Guid>();
            count = 1;
            foreach (var t in threads)
            {
                threadsLookup.Add((count++).ToString(), t.ThreadUID);
            }

            var subs = SubboardsHandler.ListSubboards(session, false);
            var subsLookup = new Dictionary<string, Guid>();
            count = 1;
            foreach (var s in subs)
            {
                subsLookup.Add((count++).ToString(), s.SubboardUID);
            }

            session["MenuLookups"] = lookups;
            session["MenuThreadLookups"] = threadsLookup;
            session["MenuSubboardLookups"] = subsLookup;

            var subboardName = string.Empty;
            var threadName = string.Empty;
            using (var mh = new ModelHelper())
            {
                mh.BeginNewContext();

                if (session.ContainsProperty("CurrentSubboardUID"))
                {
                    var subboard =
                        mh.Context.Subboards.ToList().FirstOrDefault(x => x.SubboardUID == (Guid)session["CurrentSubboardUID"]);

                    if (subboard != null)
                    {
                        subboardName = subboard.SubboardName;
                    }
                }

                if (session.ContainsProperty("CurrentThreadUID"))
                {
                    var thread =
                        mh.Context.Threads.ToList().FirstOrDefault(x => x.ThreadUID == (Guid)session["CurrentThreadUID"]);

                    if(thread != null)
                        threadName = thread.ThreadName;
                }
            }

            //if (!string.IsNullOrWhiteSpace(subboardName) &&
            //    !string.IsNullOrWhiteSpace(threadName))
            {
                Prompt = SubboardStrings.SUBBOARD_PROMPT_SIG + subboardName
                         + SubboardStrings.SUBBOARD_PROMPT_THREAD + threadName
                         + SubboardStrings.SUBBOARD_PROMPT;
            }
        }
    }
}
